from elasticsearch import Elasticsearch
import re

from flask import Blueprint,request,make_response,jsonify,redirect
from flask_restful import reqparse
from util.myjwt import myjwt
from util.captcha.captcha import captcha
import json, random, time, requests
from util.myredis import r
from util.smssdk import send_message
from models.user import *
from util.comm import get_ali_object


es_blue = Blueprint("es",__name__)
es = Elasticsearch("http://101.43.203.36:9200/")
@es_blue.route('/dockeres')
def dockeres():
    q1 = db.session.query(project).filter(project.status == 0)
    q2 = db.session.query(project1).filter(project1.status == 0)
    q3 = db.session.query(project2).filter(project2.status == 0)
    ret = q1.union(q2).union(q3).all() # 数据重复，只留一条

    for i in ret:
        es.index(index='project1',doc_type='kk',body={
            'code': str(i.code),
            'name': i.name,
            'title': i.title,
            'descs': i.descs,
            'type': i.type,
            'money': i.money,
            'mobile': i.mobile,
            'trade': i.trade,
            'status': i.status,
            'userid': i.userid,
            'create_time': str(i.create_time),
            'update_time': str(i.update_time)
        })
    # if list:
    #     for i in list:
    #         es.index(index='project',doc_type='kk',body={
    #             'code': str(i['code']),
    #             'name': i['name'],
    #             'title': i['title'],
    #             'descs': i['descs'],
    #             'type': i['type'],
    #             'money': i['money'],
    #             'mobile': i['mobile'],
    #             'trade': i['trade'],
    #             'status': i['status'],
    #             'userid': i['userid'],
    #             'create_time': str(i['create_time']),
    #             'update_time': str(i['update_time'])
    #         })
    q4 = db.session.query(ontime).filter(ontime.status == 0)
    q5 = db.session.query(ontime1).filter(ontime1.status == 0)
    q6 = db.session.query(ontime2).filter(ontime2.status == 0)
    ontimes = q4.union(q5).union(q6).all()

    for i in ontimes:
        es.index(index='ontime', doc_type='kk', body={
            'code': str(i.code),
            'name': i.name,
            'title': i.title,
            'descs': i.descs,
            'type': i.type,
            'mobile': i.mobile,
            'trade': i.trade,
            'money': i.money,
            'status': i.status,
            'userid': i.userid,
            'hire': i.hire,
            'daily': i.daily,
            'create_time': str(i.create_time),
            'update_time': str(i.update_time)
        })
    # alllist = []  # 全部数据
    # for i in list:
    #     alllist.append(i)
    # for j in dict1:
    #     alllist.append(j)
    # print('alllist', alllist)
    # if dict1:
    #     for i in dict1:
    #         es.index(index='project',doc_type='kk',body={
    #             'code': str(i['code']),
    #             'name': i['name'],
    #             'title': i['title'],
    #             'descs': i['descs'],
    #             'type': i['type'],
    #             'mobile': i['mobile'],
    #             'trade': i['trade'],
    #             'money': i['money'],
    #             'status': i['status'],
    #             'userid': i['userid'],
    #             'hire': i['hire'],
    #             'daily': i['daily'],
    #             'create_time': str(i['create_time']),
    #             'update_time': str(i['update_time'])
    #         })

    return jsonify({'code': 200})


@es_blue.route("/find")
def find():
    page = request.args.get("page")
    pagesize = 1
    start = (int(page) - 1) * pagesize
    es = Elasticsearch("http://101.43.203.36:9200/")
    sortid = request.args.get("sortid")
    type1 = request.args.get("type")
    tagid = request.args.get("tagid")
    online = request.args.get("online")
    str = '"match_all":{}'
    flag = False
    ss = '"bool":{"must":[{"match":{"'
    if type1:
        flag = True
        ss = ss + 'type":' + type1 + '}}'
    if online:
        if flag == True:
            ss = ss + ',{"match":{"online":' + online + '}}'
        else:
            flag = True
            ss = ss + 'online":' + online + '}}'
    if tagid:
        if flag == True:
            ss = ss + ',{"match":{"tagid":' + tagid + '}}'
        else:
            flag = True
            ss = ss + 'tagid":' + tagid + '}}'

    if flag == True:
        str = ss + ']}'

    if sortid:
        sort = '"sort":{"id":{"order":"desc"}}'
        dsl = '{' + '"query":{' + str + '},"from":' + str(start) + ',"size":' + str(pagesize) + ',' + sort + '}'
    else:
        dsl = '{' + '"query":{' + str + '},"from":0,"size":20}'

    match_data = es.search(index="courses", body=dsl)
    results = []
    for hit in match_data['hits']['hits']:
        results.append(hit['_source'])
    return jsonify({"res": results})

